var pageSize = 10;
var pageNum = 1;
var page;
var laypage;



$(function () {
    $("#menuId").val(sessionStorage.menuId);
    var menuId = $("#menuId").val();
    if(sessionStorage.getItem(menuId+'add') == 1){
        $("#addCycle").css('display','inline');
    }
    // alert("000"+sessionStorage.menuId);
    layui.use(['laydate','element','laypage','layer'], function(){
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        element = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层

        //以上模块根据需要引入
        var start = {
            // min: laydate.now()
            max: '2099-06-16 23:59:59'
            ,istoday: true
            ,choose: function(datas){
                end.min = datas; //开始日选好后，重置结束日的最小日期
                end.start = datas //将结束日的初始值设定为开始日
            }
        };

        var end = {
            // min: laydate.now()
            max: '2099-06-16 23:59:59'
            ,istoday: false
            ,choose: function(datas){
                start.max = datas; //结束日选好后，重置开始日的最大日期
            }
        };

        document.getElementById('LAY_demorange_s').onclick = function(){
            start.elem = this;
            laydate(start);
        }
        document.getElementById('LAY_demorange_e').onclick = function(){
            end.elem = this
            laydate(end);
        }


    });
    setTimeout(function () {
        findNewsCycleList();
    },200);

});



//查询Banner列表
function findNewsCycleList(){
    var menuId = $("#menuId").val();
    var startTime = $("#LAY_demorange_s").val();
    var endTime = $("#LAY_demorange_e").val();
    var advertType = $("#advertType").val();

    var params = {
        "type":"query",
        "menuId":menuId,
        "pageSize":pageSize,
        "pageNum":pageNum,
        "startTime":startTime,
        "endTime":endTime,
        "advertType":advertType

    };
    $.ajax({
        type:"post",
        url: "/advert/findNewsCycleList",
        data:params,
        dataType: "json",
        success : function(data){
            if(data.code = 1 && data.code != undefined){
                var list = data.newsCycles;
                page = data.page;
                var html = "";
                for(var b in list){
                    var advertId = ""
                    if(list[b].advertId != null && list[b].advertId != undefined){
                        advertId = list[b].advertId;
                    }
                    var advertName = "";
                    if(list[b].advertName != null && list[b].advertName != undefined){
                        advertName = list[b].advertName;
                    }
                    var playContent = "";
                    if(list[b].playContent != null && list[b].playContent != undefined){
                        playContent = list[b].playContent;
                    }
                    var advertType = "";
                    if(list[b].advertType == 0){
                        advertType = "普通福袋";
                    }else if(list[b].advertType == 1){
                        advertType = "优惠券福袋";
                    }else if(list[b].advertType == 2){
                        advertType = "公告";
                    }else if(list[b].advertType == 3){
                        advertType = "优惠券圈子";
                    }else if(list[b].advertType == 4){
                        advertType = "接力福袋(圈子)";
                    }else if(list[b].advertType == 5){
                        advertType = "首页广告(首页)";
                    }else if(list[b].advertType == 6){
                        advertType = "定时广告";
                    }else if(list[b].advertType == 7){
                        advertType = "运营活动广告(官方)";
                    }else if(list[b].advertType == 8){
                        advertType = "定时活动广告(官方)";
                    }else if(list[b].advertType == 9){
                        advertType = "说说(圈子)";
                    }else if(list[b].advertType == 10){
                        advertType = "公益广告";
                    }else if(list[b].advertType == 11){
                        advertType = "幸运转盘";
                    }else if(list[b].advertType == 12){
                        advertType = "提现";
                    }
                    var state = "";
                    if(list[b].state == 0){
                        state = "可播";
                    }else if(list[b].state == 1){
                        state = "不可播";
                    }
                    var topFlag = "";
                    if(list[b].topFlag == 0){
                        topFlag = "普通";
                    }else if(list[b].topFlag == 1){
                        topFlag = "置顶";
                    }
                    var source = "";
                    if(list[b].source == 0){
                        source = "app";
                    }else if(list[b].source == 1){
                        source = "后台";
                    }
                    html += '<tr><td>'+list[b].id+'</td>' +
                        '<td>'+advertId+'</td>' +
                        '<td>'+advertName+'</td>' +
                        '<td>'+playContent+'</td>' +
                        '<td>'+advertType+'</td>' +
                        '<td id="'+list[b].id+'state">'+state+'</td>' +
                        '<td>'+new Date(list[b].createTime).toLocaleString()+'</td>' +
                        '<td>'+topFlag+'</td>'+
                        '<td>'+source+'</td><td>';
                    if(sessionStorage.getItem(menuId+'edit') == 1){

                        if(list[b].state == 0){
                            html += '<button onclick="isFlag('+list[b].id+',this)" value="1" class="layui-btn layui-btn-warm">设为不可播</button>';
                        }else {
                            html += '<button onclick="isFlag('+list[b].id+',this)" value="0" class="layui-btn layui-btn-normal">设为可播</button>';
                        }
                    }
                    if(sessionStorage.getItem(menuId+'del') == 1){
                        html += '<button onclick="showDelPage('+list[b].id+')" class="layui-btn layui-btn-danger">删除</button>';
                    }
                    html += '</td></tr>';
                }
                $("#bus").html(html);
                $("#total").html("共有数据："+page.total+" 条");
                laypage({
                    cont: 'page'
                    ,pages: page.pages
                    ,first: 1
                    ,curr:pageNum
                    ,last: page.pages
                    ,prev: '<em><</em>'
                    ,next: '<em>></em>'
                    ,jump: function (obj,first) {
                        pageNum = obj.curr;
                        if(!first){
                            findNewsCycleList();
                        }
                    }
                });
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
            }else if(data.code == 2 && data.code != undefined){
                alert(data.message);
            }
        }

    });
}

//查询按钮
function search() {
    pageNum = 1;
    findNewsCycleList();
}

//打开删除询问页面
var delIndex;
function showDelPage(id) {
    var html = "<div style='text-align: center'><p style='margin-bottom: 30px'>确定删除吗?</p><button onclick='delBanner("+id+","+1+",this)' class='layui-btn layui-btn-normal'>确定</button><button onclick='delBanner("+id+","+2+",this)' class='layui-btn layui-btn-normal' style='background-color: #ff5722;margin-left: 100px'>取消</button></div>"
    delIndex = layer.open({
        title: ['提示','color: #2299ee;border-bottom: 1px solid;']
        ,type: 1
        ,content: html
        ,area: ['300px', '150px']
        ,maxmin:false
    });
}
//删除
function delBanner(id,index,obj) {
    $(obj).attr("disabled",true);
    if(index == 2){
        layer.close(delIndex);
        return false;
    }
    var menuId = $("#menuId").val();
    var params = {
        "type":"del",
        "menuId":menuId,
        "id":id
    };
    $.ajax({
        type:"post",
        url:"/advert/delNewsCycle",
        data:params,
        dataType:"json" ,
        success :function (data) {
            if(data.code == 1 && data.code != undefined){
                alert("删除成功");
                layer.close(delIndex);
                findNewsCycleList();
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
                $(obj).attr("disabled",false);
                layer.close(delIndex);
                findNewsCycleList();
            }else if(data.code == 2 && data.code != undefined){
                alert("删除失败");
                $(obj).attr("disabled",false);
                layer.close(delIndex);
                findNewsCycleList();
            }else if(data.code == 3 && data.code != undefined){
                alert("系统异常,请联系管理员");
                layer.close(delIndex);
                findNewsCycleList();
            }
        }
    });
}

//设置是否可播
function isFlag(id,obj) {
    $(obj).attr("disabled",true);

    var state = $(obj).val();
    var menuId = $("#menuId").val();
    var params = {
        "type":"change",
        "menuId":menuId,
        "id":id,
        "state":state
    };
    $.ajax({
        type:"post",
        url:"/advert/updateNewsCycle",
        data:params,
        dataType:"json" ,
        success :function (data) {

            if(data.code == 1 && data.code != undefined){
                alert("修改成功");
                if(state == 0){
                    $(obj).val(1);
                    $(obj).html("设为不可播");
                    $(obj).attr("class","layui-btn layui-btn-warm");
                    $("#"+id+"state").html("可播");
                }else if(state == 1){
                    $(obj).val(0);
                    $(obj).html("设为可播");
                    $(obj).attr("class","layui-btn layui-btn-normal");
                    $("#"+id+"state").html("不可播");
                }
                $(obj).attr("disabled",false);
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
                $(obj).attr("disabled",false);
            }else if(data.code == 2 && data.code != undefined){
                alert("修改失败");
                $(obj).attr("disabled",false);
            }else if(data.code == 3 && data.code != undefined){
                alert("系统异常,请联系管理员");
                $(obj).attr("disabled",false);
            }
        }
    });
}
//打开添加页面
var addIndex;
function showAddPage() {
    var html = '<table style="border-collapse: separate;border-spacing: 10px"><tr><td style="float: right">播放内容</td><td><input id="playContent" style="height: 30px;width: 260px" placeholder="播放内容(必填)"/></td></tr>' +
        '<tr><td style="float: right">是否置顶</td><td><input type="radio" name="topFlag" style="width: 40%;height: 30px;" checked="true" value="0"/>否<input type="radio" name="topFlag" style="width: 40%;height: 30px;" value="1"/>是</td></tr>'+
        '<tr><td style="float: right">广告ID</td><td><input id="advertId" style="height: 30px;width: 260px" placeholder="广告ID(选填)"/></td></tr>'+
        "<tr><td style='float: right'>播放次数</td><td><input id='playCount' style='height: 30px;width: 260px' placeholder='播放次数(选填)'/></td></tr></table>"+
        "<button onclick='addCycle(this)' class='layui-btn layui-btn-normal' style='float: right;margin-right: 10px;margin-top: 57px'>提交</button>";
    addIndex = layer.open({
        title: ['添加走马灯信息','color: #2299ee;border-bottom: 1px solid;']
        ,type: 1
        ,content: html
        ,area: ['600px', '400px']
        ,maxmin:true
    });
}

//添加走马灯
function addCycle(obj) {
    $(obj).attr("disabled",true);
    var menuId = $("#menuId").val();
    var playContent = $("#playContent").val();
    if(playContent == "" || playContent == undefined){
        alert("播放内容不能为空");
        $(obj).attr("disabled",false);
        return false;
    }
    var topFlag = $("input[name=topFlag]:checked").val();
    var advertId = $("#advertId").val();
    var playCount = $("#playCount").val();
    if(isNaN(playCount)){
        alert("播放次数填写错误");
        $(obj).attr("disabled",false);
        return false;
    }

    var params = {
        "type":"add",
        "menuId":menuId,
        "playContent":playContent,
        "topFlag":topFlag,
        "advertId":advertId,
        "playCount":playCount
    };


    $.ajax({
        type:"post",
        url:"/advert/addNewsCycle",
        data:params,
        dataType:"json" ,
        success :function (data) {

            if(data.code == 1 && data.code != undefined){
                alert("添加成功");
                layer.close(addIndex);
                findNewsCycleList();
            }else if(data == 9999 && data != undefined){
                window.location.href = "/login_toLogin";
            }else if(data == 1111 && data != undefined){
                alert("无权操作");
                $(obj).attr("disabled",false);
                layer.close(addIndex);
                findNewsCycleList();
            }else if(data.code == 2 && data.code != undefined){
                alert("添加失败");
                $(obj).attr("disabled",false);
                layer.close(addIndex);
                findNewsCycleList();
            }else if(data.code == 3 && data.code != undefined){
                alert("系统异常,请联系管理员");
                layer.close(addIndex);
                findNewsCycleList();
            }
        }
    });
}